<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use app\api\model\NewsCategoryModel;
use app\api\model\UserModel;
use library\Controller;
use library\tools\Data;
use think\Db;

/**
 * 主播管理
 * Class Anchor
 * @package app\store\controller
 */
class Anchor extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'te_anchor';

    /**
     * 主播列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '主播管理';
        $params = input();
        $where = [];
        #关键词搜索
        if (isset($params['keywords']) && $params['keywords'] != ''){

            $where[] = ['n.link|c.name', 'like',"%{$params['keywords']}%"];
        }

        #主播分类搜索
        if (isset($params['cate_id']) && $params['cate_id'] != ''){

            $where[] = ['n.cate_id', 'eq',$params['cate_id']];
        }


        #时间筛选
        if (isset($params['create_time']) && $params['create_time'] !== '') {
            $create_time = explode(' - ',$params['create_time']);
            $where[] = ['n.create_at','between',"{$create_time[0]},{$create_time[1]}"];
        }

        $query = $this->_query($this->table)->where($where)
            ->alias('n')
            ->join('te_category c','n.cate_id =  c.id')->field('n.*,c.name');

        $query->dateBetween('n.create_at')->where(['n.is_deleted'=>0])->order('n.id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {

        $this->news_category = NewsCategoryModel::newsCateList();
    }

    /**
     * 主播
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '主播信息';
        $this->isAddMode = '1';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑主播
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑主播';
        $this->isAddMode = '0';
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _form_filter(&$data)
    {

        if ($this->request->isGet()){

            $category = Db::name('te_category')->where(['status'=>1,'is_deleted'=>0])->select();
            if ($category){

//                foreach ($category as &$vo) {
//                    $vo['ids'] = join(',', Data::getArrSubIds($data, $vo['id']));
//                }
                $this->category = Data::arr2table($category);

            }else{

                $this->category = [];
            }

        }


    }

    /**
     * 禁用主播
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用主播
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 删除主播
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('操作成功！', 'javascript:history.back()');
        }
    }

    
}
